<?php
	include_once 'isLogin.php';
	$add_success = null;
	$table_name = "company_levequestion";
	$table= "company_levequestion";
	if ($_GET['upid']!=''&&$_GET['level']!=null) {
	     $level = $_GET['level'];
	     $upid = $_GET['upid'];
		 $categories=$_GET['categories'];
	}
//判断添加问题的序号是否存在
if($_POST['submit']){
	$sql=$table." where list_id=".$_POST['list']." and title_level=1 and categories_id=".$_GET['id'];
	$title=$commonc->getList($sql, $sqlnum='', $key="*",$debug='0');
		if(isset($title['result'])){
			echo '<script type="text/javascript">'."\n";
			//echo 'history.back();'."\n";
            echo '</script>'."\n";
			$data=array('conent'=>$_POST['conent']);
			$result=$commonc->update($table_name,$data,"categories_id=".$_GET['id']." and list_id=".$_POST['list']);
			$add_success = 2;
		}else{
			$datas=array('list_id'=>$_POST['list'],'conent'=>$_POST['conent'],'categories_id'=>$_GET['id'],'title_level'=>'1');
			$commonc->insert($table_name,$datas);
			$add_success = 1;
		}
		echo $commonc->submitAlert('成功',$href);
	}
	if($_GET['action']=='add'){
	
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
		
		if($id!=''){
			$return='<tr line_id="'.$id.'">
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td value_id="'.$id.'">'.$data['title'].'</td>
			<td value_id="'.$id.'">'.$data['content'].'</td>
			<td>&nbsp;</td>
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
			$return.='</td></tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}
		echo json_encode($arr);
		exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		
		echo $commonc->del($table_name,$_POST['id']);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}
	$smarty->assign('add_success',$add_success);
	$smarty->assign('res',$res['result']);
	$smarty->assign('result',$result['result']);
	$smarty->display($_filename.".html");
	
?>